<?php

namespace Api\Model;

use Think\Model;

class MemberModel extends Model {

    protected $tableName = 'classmember';

//group:班主任10，老师3，家长：2;学生：1；
    function addMember($user_id, $class_id, $rname, $remarks = '', $power = 0) {
        $data = array(
            'class_id' => $class_id,
            'user_id' => $user_id,
            'rename' => $rname,
            'remarks' => $remarks,
            'power' => $power,
            'createtime' => date('Y-m-d H:i;s'),
        );
        $this->add($data);
    }

    function upMemberbyids($user_id, $class_id, $updata) {
        $where = array(
            'user_id' => $user_id,
            'class_id' => $class_id
        );
        return $this->where($where)->save($updata);
    }

    function getList($class_id, $start, $limit = 30) {
        $limit = $start . ',' . $limit;
        $where = array('class_id' => $class_id);
        return $this->where($where)->limit($limit)->select();
    }

    function getuList($user_id, $class_id, $start, $limit = 30) {
        $limit = $start . ',' . $limit;
        return $this->where("user_id<>%d and class_id=%d", array($user_id, $class_id))->limit($limit)->select();
    }

    function getMembyids($user_id, $class_id) {
        $where = array(
            'class_id' => $class_id,
            'user_id' => $user_id
        );
        return $this->where($where)->find();
    }

    function getClassesbyUserid($user_id) {
        $where = array('user_id' => $user_id);
        return $this->where($where)->field('class_id')->limit(5)->select();
    }

    function getClasslistbyuid($user_id) {
        $where = array('jl_classmember.user_id' => $user_id);
        return $this->join('__CLASS__ on __CLASS__.id=__CLASSMEMBER__.class_id')->where($where)->field('jl_classmember.class_id as classid,jl_classmember.user_id as userid,jl_class.classname as classname,jl_class.code as code,jl_class.description as description,jl_classmember.power as power')->limit(5)->select();
    }

    function getCountbycid($class_id) {
        $where = array('class_id' => $class_id);
        return $this->where($where)->count('id');
    }

    function delMemberbyids($user_id, $class_id) {
        $where = array(
            'class_id' => $class_id,
            'user_id' => $user_id
        );
        return $this->where($where)->delete();
    }

    function getReplybyids($class_id, $post_id, $start = 0, $limit = 40) {
        $where = 'jl_reply.post_id =' . $post_id . ' AND jl_classmember.class_id =' . $class_id;
        $join = 'LEFT JOIN  __REPLY__ ON __CLASSMEMBER__.user_id = __REPLY__.user_id';
        $field = 'jl_classmember.rename as name,jl_reply.icon as icon,jl_reply.createtime as time';
        $limit = $start . ',' . $limit;
        return $this->where($where)->field($field)->join($join)->limit($limit)->select();
    }

    function getCmembersbyCid($cid, $start = 0, $limit = 40) {
        $map['power'] = array('GT', 0);
        $map['class_id'] = array('EQ', $cid);
        return $this->where($map)->limit($start, $limit)->select();
    }

    function getCmemberCbyids($iUid, $iCid) {
        $where = array(
            'jl_classmember.class_id' => $iCid,
            'jl_classmember.user_id' => $iUid
        );
        $join = array(
            '__CLASS__ ON __CLASSMEMBER__.class_id=__CLASS__.id',
            'LEFT JOIN __USERS__ ON __CLASSMEMBER__.user_id=__USERS__.id',
            'LEFT JOIN __USERINFO__ ON __CLASSMEMBER__.user_id=__USERINFO__.user_id');

        $field = 'jl_classmember.class_id as class_id,jl_classmember.user_id as uid,jl_classmember.rename as name'
                . ',jl_classmember.remarks as remarks,jl_classmember.power as power,jl_classmember.createtime as time'
                . ',jl_class.code as code,jl_class.classname as classname,jl_users.username,jl_users.email as email'
                . ',jl_users.group,jl_users.createtime as createtime,jl_userinfo.user_fname,jl_userinfo.user_lname'
                . ',jl_userinfo.user_aname,jl_userinfo.user_tel,jl_userinfo.user_gender,jl_userinfo.user_address,jl_userinfo.user_tel';
        return $this->where($where)->join($join)->field($field)->find();
    }

    function getmListbycid($iCid, $iStart, $iLimit = 30) {
        $where = array('jl_classmember.class_id' => $iCid);
        $field = 'jl_classmember.class_id,jl_classmember.user_id'
                . ',jl_classmember.rename,jl_classmember.power as power'
                . ',jl_users.group,jl_users.username';
        $join = 'LEFT JOIN __USERS__ ON __CLASSMEMBER__.user_id=__USERS__.id';
        return $this->where($where)->join($join)->field($field)->limit($iStart, $iLimit)->select();
    }

}
